package com.iyouqu.dycm.search.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import com.iyouqu.dycm.search.utils.redis.CustomPool;
import com.iyouqu.dycm.search.utils.redis.RedisSentinelPoolHolder;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;

public class RedisUtil {

	private static CustomPool getPool() {
		return RedisSentinelPoolHolder.getInstance();
	}

	private static void returnResource(CustomPool pool, Jedis jedis) {
		if (jedis != null) {
			pool.returnResource(jedis);
		}
	}

	/**
	 * 
	 * @param key.
	 * @return bol.
	 */
	public static boolean keyExist(String key) {
		boolean value = false;
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.exists(key);
		} catch (Exception e11) {
			e11.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 
	 * @param key.
	 * @return bol.
	 */
	public static boolean keyExist(byte[] key) {
		boolean value = false;
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.exists(key);
		} catch (Exception e11) {
			e11.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 
	 * @param key.
	 * @return long.
	 */
	public static long getListLength(String key) {
		long value = 0;
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.llen(key);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 
	 * @param key.
	 * @return bol.
	 */
	public static String get(String key) {
		String value = null;
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (JedisConnectionException e11) {
			try {
				value = jedis.get(key);
			} catch (Exception e1) {
				e1.printStackTrace();
				throw new RuntimeException(e1);
			}
		} catch (Exception e22) {
			e22.printStackTrace();
			throw new RuntimeException(e22);
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 
	 * @param key.
	 * @return bs.
	 */
	public static byte[] get(byte[] key) {
		byte[] value = null;
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (JedisConnectionException e11) {
			try {
				value = jedis.get(key);
			} catch (Exception e1) {
				e1.printStackTrace();
				throw new RuntimeException(e1);
			}
		} catch (Exception e22) {
			e22.printStackTrace();
			throw new RuntimeException(e22);
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 获取有序集合中的start-end的成员
	 * 
	 * @param key.
	 * @param start.
	 * @param end.
	 * @return set bs.
	 */
	public static Set<byte[]> get(byte[] key, int start, int end) {
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			return jedis.zrevrange(key, start, end);
		} catch (Exception e11) {
			System.out.println(e11);
			return null;
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 
	 * @param key.
	 * @return obj.
	 */
	public static <T> T getObject(String key, Class<T> clazz) {
		String jsonString = get(key);
		if (jsonString == null) {
			return null;
		}
		return JSONObject.parseObject(jsonString, clazz);
	}

	/**
	 * 
	 * @param key.
	 */
	public static void set(String key, Object value) {
		String jsonString = value == null ? null : JSONObject.toJSONString(value);
		set(key, jsonString);
	}

	/**
	 * 
	 * @param key
	 *            val.
	 */
	public static void set(String key, String value) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.set(key, value);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 
	 * @param key.
	 * @param value.
	 */
	public static void set(byte[] key, byte[] value) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.set(key, value);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static void setex(String key, Object value, int seconds) {
		String jsonString = value == null ? null : JSONObject.toJSONString(value);
		setex(key, jsonString, seconds);
	}

	/**
	 * 
	 * @param key.
	 * @param value.
	 * @param seconds.
	 */
	public static void setex(String key, String value, int seconds) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.setex(key, seconds, value);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 
	 * @param key.
	 * @param value.
	 * @param seconds.
	 */
	public static void setex(byte[] key, byte[] value, int seconds) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.setex(key, seconds, value);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * @param key.
	 * @param seconds.
	 */
	public static void expire(String key, int seconds) {
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.expire(key, seconds);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 
	 * @param key.
	 * @param seconds.
	 */
	public static void expire(byte[] key, int seconds) {
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.expire(key, seconds);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 
	 * @param key.
	 */
	public static void del(String key) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.del(key);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 
	 * @param key.
	 */
	public static void del(byte[] key) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.del(key);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 删除有序集合中的成员
	 * 
	 * @param key.
	 * @param members.
	 */
	public static void del(byte[] key, byte[]... members) {
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.zrem(key, members);
		} catch (Exception e11) {
			System.out.println(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 
	 * @param key.
	 * @param value.
	 */
	public static void push(String key, String value) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.lpush(key, value);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 
	 * @param key.
	 * @param values.
	 */
	public static void push(String key, List<String> values) {
		CustomPool pool = null;
		Jedis jedis = null;
		int length = values.size();
		String[] vs = new String[length];
		for (int i = 0; i < length; i++) {
			vs[i] = values.get(i);
		}

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.lpush(key, vs);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * @param key.
	 * @return str.
	 */
	public static String pop(String key) {
		String value = null;
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.rpop(key);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * @param key.
	 * @param members.
	 */
	public static void addSetValue(String key, String... members) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.sadd(key, members);
		} catch (Exception e11) {
			//
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 
	 * @param key.
	 * @return setstr.
	 */
	public static Set<String> getSetValues(String key) {
		Set<String> value = null;
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.smembers(key);
		} catch (Exception e11) {
			//
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * @doc 判断是否存在于key对应的set集合中
	 * @param key.
	 * @param member
	 *            .
	 * @return bol.
	 */
	public static boolean isExistInSet(String key, String member) {
		boolean result = false;
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.sismember(key, member);
		} catch (Exception e11) {
			//
		} finally {
			returnResource(pool, jedis);
		}
		return result;
	}

	/**
	 * @doc 判断是否存在于key对应的set集合中
	 * @param key.
	 * @param members.
	 * @return bol.
	 */
	public static long removeValueInSet(String key, String... members) {
		long result = 0L;
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.srem(key, members);
		} catch (Exception e1) {
			//
		} finally {
			returnResource(pool, jedis);
		}
		return result;
	}

	/**
	 * @param key.
	 * @param count.
	 * @return list str.
	 */
	public static List<String> pops(String key, int count) {
		List<String> value = new ArrayList<String>();
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			for (int i = 0; i < count; i++) {
				String result = jedis.rpop(key);
				if (result != null) {
					value.add(result);
				} else {
					break;
				}
			}
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 
	 * @param channel.
	 * @param message.
	 */
	public static void publish(String channel, String message) {
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.publish(channel, message);
		} catch (Exception e11) {
			e11.printStackTrace();
			throw new RuntimeException(e11);
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 有序集合中添加成员
	 * 
	 * @param key.
	 * @param scoreMembers.
	 */
	public static void sortAdd(byte[] key, Map<byte[], Double> scoreMembers) {
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.zadd(key, scoreMembers);
		} catch (Exception e11) {
			e11.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
	}

	/**
	 * 有序集合中的成员总数
	 * 
	 * @param key.
	 * @param members.
	 */
	public static long getCount(byte[] key) {
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			return jedis.zcard(key);
		} catch (Exception e11) {
			System.out.println(e11);
			return 0L;
		} finally {
			returnResource(pool, jedis);
		}
	}
}
